﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace BaseHelper.Base
{
    public class ObjectToFileHelper
    {
        public static void Save(string file, object obj)
        {
            try
            {
                using (FileStream fs = new FileStream(file, FileMode.Create))
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    bf.Serialize(fs, obj);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Err("BaseHelper.Base.ObjectToFileHelper.cs", "Save()", ex);

            }
        }

        public static object Load(string file)
        {
            object tmp = null;
            if (!File.Exists(file))
                return null;

            try
            {
                using (FileStream fs = new FileStream(file, FileMode.Open))
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    tmp = bf.Deserialize(fs);
                }

            }
            catch (Exception ex)
            {
                LogHelper.Err("BaseHelper.Base.ObjectToFileHelper.cs", "Save()", ex);
            }

            return tmp;
        }
    }
}
